package com.ruoyi.web.controller.ability;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.ruoyi.common.MinioFileUtils;
import com.ruoyi.common.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@RestController
@RequestMapping("/system/static")
@Api(tags = "文件上传")
public class StaticResourceController {

    private static GsonBuilder gsonBuilder = new GsonBuilder();
    private static final Gson gson = gsonBuilder.create();
    @Autowired
    private MinioFileUtils minioFileUtils;
    private static final Logger logger = LoggerFactory.getLogger(StaticResourceController.class);

    /**
     * 上传文件
     *
     * @param file 文件对象
     * @return 文件访问地址
     * @throws IOException
     */
    @RequestMapping(value = "/uploadImg", method = RequestMethod.POST, consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    @ApiOperation(value = "上传图片", httpMethod = "POST")
    public Result upload(@RequestPart("file") MultipartFile file, HttpServletRequest request) throws IOException {
        return minioFileUtils.upload(file, true);

    }

    private void setContent(HttpServletResponse httpServletResponse, Result result) throws IOException {
        httpServletResponse.setHeader("Access-Control-Allow-Origin", "*");
        httpServletResponse.setHeader("Cache-Control", "no-cache");
        httpServletResponse.setContentType(MediaType.APPLICATION_JSON_UTF8_VALUE);
        String json = gson.toJson(result);
        httpServletResponse.getWriter().write(json);
        httpServletResponse.flushBuffer();
    }
}
